﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using System.Text;
using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;
using He.Common;
using System.Text.RegularExpressions;

namespace He.Web.manage.collect
{
    public partial class custom : Manage
    {
        public BLL.channel cbll = new BLL.channel();
        public BLL.article abll = new BLL.article();
        RemoteHelper rh = new RemoteHelper();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.p1.Visible = true;
                this.p2.Visible = false;
                this.p3.Visible = false;

                ddlLang.DataSource = LangHelper.GetLangDict();
                ddlLang.DataTextField = "Value";
                ddlLang.DataValueField = "Key";
                ddlLang.DataBind();

                this.BindChannel();
                base.CategoryTreeBind(0, "", int.Parse(this.ddlChannel.SelectedValue), this.ddlCategory);
            }
        }

        private void BindChannel()
        {
            this.ddlChannel.DataSource = this.cbll.GetList(string.Format("lang='{0}' and is_lock=0 order by sort_id", this.ddlLang.SelectedValue));
            this.ddlChannel.DataTextField = "title";
            this.ddlChannel.DataValueField = "id";
            this.ddlChannel.DataBind();
        }

        protected void btnBack1_Click(object sender, EventArgs e)
        {
            this.p1.Visible = true;
            this.p2.Visible = false;
            this.p3.Visible = false;
        }

        protected void btnBack2_Click(object sender, EventArgs e)
        {
            this.p1.Visible = false;
            this.p2.Visible = true;
            this.p3.Visible = false;
        }

        protected void btnCaiji_Click(object sender, EventArgs e)
        {
            string[] urls = this.txtMulUrl.Text.Replace("\r\n", "|").Split(new char[] { '|' });
            int cateId = int.Parse(this.ddlCategory.SelectedValue);
            Encoding listEncoding = Encoding.GetEncoding(this.ddlListEncode.SelectedValue);
            Encoding detailEncoding = Encoding.GetEncoding(this.ddlDetailEncode.SelectedValue);
            string aSelector = this.txtASelector.Text.Trim();
            string detailSelector = this.txtDetailSelector.Text.Trim();
            string domain = this.txtDomain.Text.Trim();
            int sum = 0;
            foreach (string url in urls)
            {
                this.SpiderByListUrl(listEncoding, detailEncoding, url, aSelector, detailSelector, domain, cateId, ref sum);
            }
            this.litMsg.Text = string.Format("<b style='color:red'>成功采集{0}条数据 {1}</b>", sum, DateTime.Now);
        }

        protected void btnTestDetail_Click(object sender, EventArgs e)
        {
            Encoding detailEncoding = Encoding.GetEncoding(this.ddlDetailEncode.SelectedValue);
            string detailUrl = this.txtDetailUrl.Text.Trim();
            string detailSelector = this.txtDetailSelector.Text;
            if (string.IsNullOrEmpty(detailSelector))
            {
                ShowError("信息填写不完整");
            }
            else
            {
                string html = RemoteHelper.GetRemoteHtml(detailUrl, detailEncoding);
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);
                HtmlNode node = doc.DocumentNode.QuerySelector(detailSelector);
                if (node != null)
                {
                    this.litMsg.Text = this.ProcessPic(node.InnerHtml, this.txtDomain.Text.Trim());
                    this.p1.Visible = false;
                    this.p2.Visible = false;
                    this.p3.Visible = true;
                    this.txtMulUrl.Text = this.txtListUrl.Text;
                }
                else
                {
                    this.litMsg.Text = "没有找到相应的结点，选择器有误或目标网站html不规范";
                }
            }
        }

        protected void btnTestList_Click(object sender, EventArgs e)
        {
            Encoding listEncoding = Encoding.GetEncoding(this.ddlListEncode.SelectedValue);
            string listUrl = this.txtListUrl.Text.Trim();
            string aSelector = this.txtASelector.Text.Trim();
            string domain = this.txtDomain.Text.Trim();
            if (string.IsNullOrEmpty(listUrl) || string.IsNullOrEmpty(aSelector))
            {
                base.ShowError("信息填写不完整");
            }
            else
            {
                string html = RemoteHelper.GetRemoteHtml(listUrl, listEncoding);
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);
                IEnumerable<HtmlNode> nodeList = doc.DocumentNode.QuerySelectorAll(aSelector);
                if (nodeList != null)
                {
                    List<Article> articleList = new List<Article>();
                    foreach (HtmlNode node in nodeList)
                    {
                        Article model = new Article
                        {
                            Url = domain + node.Attributes["href"].Value.ToString(),
                            Title = node.InnerText
                        };
                        articleList.Add(model);
                    }
                    this.ddlDetailUrl.DataSource = articleList;
                    this.ddlDetailUrl.DataTextField = "Title";
                    this.ddlDetailUrl.DataValueField = "Url";
                    this.ddlDetailUrl.DataBind();
                    this.txtDetailUrl.Text = this.ddlDetailUrl.SelectedValue;
                    this.ddlDetailEncode.SelectedValue = this.ddlListEncode.SelectedValue;
                    this.p1.Visible = false;
                    this.p2.Visible = true;
                    this.p3.Visible = false;
                }
            }
        }

        protected void ddlChannel_SelectedIndexChanged(object sender, EventArgs e)
        {
            base.CategoryTreeBind(0, "", int.Parse(this.ddlChannel.SelectedValue), this.ddlCategory);
        }

        protected void ddlDetailUrl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtDetailUrl.Text = this.ddlDetailUrl.SelectedValue;
        }

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindChannel();
        }

        public string GetContent(string url, Encoding encoding, string selector)
        {
            string html = RemoteHelper.GetRemoteHtml(url, encoding);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            HtmlNode node = doc.DocumentNode.QuerySelector(selector);
            if (node == null)
            {
                return "";
            }
            return node.InnerHtml;
        }

        private string ProcessPic(string body, string siteUrl)
        {
            string imgReg = "(?<=<img.+?src\\s*?=\\s*?\"?)([^\\s\"]+?)(?=[\\s\"])";
            siteUrl = siteUrl.Trim(new char[] { '/' });
            MatchCollection matchs = Regex.Matches(body, imgReg, RegexOptions.Singleline);
            for (int i = 0; i < matchs.Count; i++)
            {
                string imgUrl = matchs[i].Groups[1].Value;
                if (!imgUrl.StartsWith("http://"))
                {
                    string newImgUrl = siteUrl + "/" + imgUrl.Trim(new char[] { '/' });
                    body = body.Replace(imgUrl, newImgUrl);
                }
            }
            return body;
        }

        private void SpiderByListUrl(Encoding listEncoding, Encoding detailEncoding, string listUrl, string aSelector, string detailSelector, string domain, int cateId, ref int sum)
        {
            string html = RemoteHelper.GetRemoteHtml(listUrl, listEncoding);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            IEnumerable<HtmlNode> nodeList = doc.DocumentNode.QuerySelectorAll(aSelector);
            if (nodeList != null)
            {
                List<Article> articleList = new List<Article>();
                foreach (HtmlNode node in nodeList)
                {
                    Model.article model = new Model.article();
                    string detailUrl = domain + node.Attributes["href"].Value.ToString();
                    model.category_id = cateId;
                    model.title = node.InnerText.Trim();
                    model.seo_keywords = model.title;
                    model.seo_desc = model.title;
                    model.content = this.GetContent(detailUrl, detailEncoding, detailSelector);
                    model.content = this.ProcessPic(model.content, this.txtDomain.Text.Trim());
                    if (!string.IsNullOrEmpty(model.content))
                    {
                        sum++;
                        this.abll.Add(model);
                    }
                }
            }
        }

        // Nested Types
        public class Article
        {
            // Properties
            public string Content { get; set; }

            public string Title { get; set; }

            public string Url { get; set; }
        }

    }
}